package com.example.yamba;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Application;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;
import android.util.Log;

import com.sina.weibo.sdk.openapi.StatusesAPI;
import com.sina.weibo.sdk.openapi.models.Status;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
public class YambaApplication extends Application implements OnSharedPreferenceChangeListener{
	private static final String TAG = YambaApplication.class.getSimpleName();
	private static final String pattern = "EEE MMM dd HH:mm:ss Z yyyy";
	private static SimpleDateFormat  format = new SimpleDateFormat (pattern,new Locale("en"));
	private SharedPreferences statusPref ;
	private MyStatusesAPI statusesAPI = null;
	private StatusData statusData;
	private boolean isRunning = false;
	public void onCreate(){
		super.onCreate();
		statusPref = PreferenceManager.getDefaultSharedPreferences(this);
		statusPref.registerOnSharedPreferenceChangeListener(this);
		statusData = new StatusData(this);
		Log.i(TAG,"YambaApplication onCreated");
	}
	@Override
	public void onTerminate() {
		// TODO Auto-generated method stub
		super.onTerminate();
		Log.i(TAG,"Application onTerminated");
	}
	@Override
	public synchronized void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// TODO Auto-generated method stub
		Log.i(TAG,"Application's pref changed");
	}
	public synchronized MyStatusesAPI getStatusesAPI(){
		Oauth2AccessToken token = AccessTokenKeeper.readAccessToken(this);
		if(statusesAPI==null){
			statusesAPI = new MyStatusesAPI(this,Constants.APP_KEY,token);
		}
		return statusesAPI;
	}
	public StatusData getStatusData(){
		return statusData;
	}
	public static SimpleDateFormat getDateFormat(){
		return format;
	}
	public synchronized int fetchStatusUpdate(){
		try{
			String strList = this.getStatusesAPI().userTimelineSync(0L, 0L, 20, 1);
			JSONObject e = new JSONObject(strList);
			JSONArray jsonArray = e.getJSONArray("statuses");
			Log.i(TAG,"Thread ran size="+jsonArray.length());
			int count = 0;
			long lastTime = this.statusData.getLastestStatusCreateAtTime();
			ContentValues values = new ContentValues();
			for(int i=0; i<jsonArray.length();i++){
				JSONObject json = jsonArray.getJSONObject(i);
				Status s = Status.parse(json);
				values.clear();
				values.put(DBHepler.C_ID, json.optLong("id"));
				values.put(DBHepler.C_CREATE_AT,String.valueOf(format.parse(s.created_at).getTime()));
				values.put(DBHepler.C_USER,s.user.screen_name);
				values.put(DBHepler.C_STATUS,json.toString());
				statusData.insertOrIgnore(values);
				long time = YambaApplication.getDateFormat().parse(json.getString("created_at")).getTime();
				if(lastTime<time){
					count++;
				}
				//Log.d(TAG, String.format("%s: %s", s.user.screen_name, s.text));
			}
			return count;
		}catch(JSONException je){
			je.printStackTrace();
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return 0;
	}

	public boolean isRunning() {
		return isRunning;
	}

	public void setIsRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}
}
